(function(){
    if ($.validity) {
        $.validity.outputs.custom = {
            start:function(){
            },
            end:function(results) {
            },
            raise:function($obj, msg){
                //$obj.each(function(){
                //    var tip=new Opentip($(this), msg, { target:$(this),tipJoint: 'bottom',removeElementsOnHide:true,showOn:'creation' });
                //    tip.show();
                //}); tipJoint: 'bottom',
                var tip=new Opentip($obj, msg, { target:$obj,removeElementsOnHide:true,showOn:'creation' });
                tip.show();
            },
            raiseAggregate:function($obj, msg){
                this.raise($($obj.get($obj.length-1)), msg); 
            }
        };
    }
})();

$(function() {
    if ($.validity) {
        // Now enable the output mode we just installed.
        $.validity.setup({ outputMode:'custom' });
    }
    if ($('form') && $('form').length > 0 && $('form').mothinForm) {
        $('form').mothinForm();
    }
    doNameDisplay();
    setTimeout(function() {
        $(":text:visible:first").focus();
    }, 100);
});

// 0 - id名称显示 根据Id显示对象名称（删除中划线）
function doNameDisplay(){
    if($(".nameDisplay").length>0){
        var maps=[];
        $(".nameDisplay").each(function(){
            var id=$(this).attr("itemId");
            if($.inArray(id,maps)>=0)return;
            maps.push(id);

            var action="common/display!query.action?id="+id+"&_="+$.mothin.util.getStamp();
            $.ajax({
                url: action,
                dataType:"json",
                success:function(data){
                    if(data){
                        var txt=data.name;
                        if(data.deleted){
                            txt="<span style='text-decoration:line-through;'>"+data.name+"</span>";
                        }
                        $(".nameDisplay[itemId='"+id+"']")
                            .removeClass("nameDisplay")
                            .removeAttr("itemId")
                            .append(txt);
                    }else{
                        $(".nameDisplay[itemId='"+id+"']").remove();
                    }
                }
            });
        });
    }
}

// 1-页面跳转
function doNavigate(url, params) {
    if (params) {
        if (url.indexOf('?') < 0) {
            url += '?_=' + Date.parse(new Date());
        } else {
            url += '&_=' + Date.parse(new Date());
        }
        for ( var key in params) {
            url += '&' + key + '=' + params[key];
        }
    }

    if (url.indexOf('http') >= 0) {
        window.location.href = url;
    } else {
        var base = $('base[href]').attr('href');
        if (base) {
            window.location.href = base + url;
        } else {
            window.location.href = url;
        }
    }
}

// 2-关闭弹出框
function closeDialog() {
    if (window.parent) {
        var doc = $('html').html();
        window.parent.$('iframe').each(function() {
            if (window.parent.$(this).contents().find("html").html() == doc) {
                window.parent.$(window.parent.$(this).parent()).window('close');
            }
        });
    }
}

// 3-调整当前弹出框的width与height
function changeDialogWH(w, h) {
    if (!w) {
        w = 800;
    }
    if (!h) {
        h = 600;
    }
    if (window.parent) {
        var doc = $('html').html();
        window.parent.$('iframe').each(function() {
            if (window.parent.$(this).contents().find("html").html() == doc) {
                var id = 'div_' + window.parent.$(this).attr('id').substring(0, 7);
                window.parent.$('#' + id).window('resize', {
                    width : w,
                    height : h
                });
            }
        });
    }
}

// 4-配置view菜单栏
function configOperation_panel(panelId, withBack, callback) {
    var data = frame_queryOperation(History.getState().url);
    if (data) {
        if (data.tools && data.tools.length > 0) {
            var value = data.tools;
            var id = "toolbarDiv_" + $.mothin.util.newGuid();
            var toolbar = $('<div id="' + id + '" class="datagrid-toolbar"/>');
            var tools = $('<div style="margin:5px;text-align:right;"/>');
            toolbar.append(tools);
            $($('#' + panelId).children()[0]).before(toolbar);
            creatTools(value, tools, callback);
        }
    }
    if (withBack) {
        var backDiv = $('<div class="text-center" style="padding:5px;" />');
        var backButton = $('<button class="btn btn-link" onclick="History.back();"><span class="glyphicon glyphicon-share-alt"></span>返回</button>');
        $('#' + panelId).panel('body').append(backDiv.append(backButton));
    }
}

// 5-获取相关操作项
function frame_queryOperation(url) {
    var action = url;
    var index = action.indexOf('?');
    if (index >= 0) {
        action = action.substring(0, index);
    }

    var prefix = 'query_operations_';
    var value = locache.get(prefix + action);
    if (value == undefined) {
        $.ajax({
            url : 'common/user_helper!queryOperation.action',
            data : {
                url : action
            },
            async : false,
            dataType : "json",
            success : function(data) {
                if (data.ok) {
                    locache.set(prefix + action, data.value);
                    value = data.value;
                }
            }
        });
    }
    return value;
}

// -----------------LOCAL STORAGE OPERATION---------------------------
// login_user 登录用户信息，包含姓名及是否超级用户
// default_pager_size* 默认的列表分页长度
// userForRemember* 自动登录账号及密码

// 8.1 缓存保存
function saveStorage(key, value) {
    locache.set(key, value);
}

// 8.2 缓存读取
function getStorage(key) {
    return locache.get(key);
}

// 8.3-保存登录用户姓名
function saveLoginUser(user) {
    locache.set('login_user', user);
}

// 8.4-获取登录用户姓名
function getLoginUserName() {
    var user = locache.get('login_user');
    if (user && user.name) {
        return user.name;
    }
    return '';
}

// 8.5-获取登录用户是否超级用户
function isLoginUserSuper() {
    var user = locache.get('login_user');
    if (user && user.superUser) {
        return true;
    }
    return false;
}

// 8.6-获取保存的列表默认长度
function getDefaultPagerSize() {
    var size = locache.get('default_pager_size');
    if (!size) {
        size = 10;
    }
    return size;
}

// ---与框架程序做适应性接口------
// 1-弹出层
function showDialog(url, title, width, height, callBack_onClose) {
    if (top && top != window) {
        top.__showDialog(url, title, width, height, callBack_onClose);
    } else {
        $script('res/js/project/frame.js', function() {
            __showDialog(url, title, width, height, callBack_onClose);
        });
    }
}

// 2-提示信息
function showMessage(content, callback, type) {
    if (top && top != window) {
        top.__showMessage(content, callback, type);
    } else {
        $script('res/js/project/frame.js', function() {
            __showMessage(content, callback, type);
        });
    }
}

// 3-确认信息
function showConfirm(content, callback, yes, no) {
    if (top && top != window) {
        top.__showConfirm(content, callback, yes, no);
    } else {
        $script('res/js/project/frame.js', function() {
            __showConfirm(content, callback, yes, no);
        });
    }
}

// 4-确认信息后自定义操作
function makeChoice(content, yes, no, auto) {
    if (top && top != window) {
        top.__makeChoice(content, yes, no, auto);
    } else {
        $script('res/js/project/frame.js', function() {
            __makeChoice(content, yes, no, auto);
        });
    }
}